﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Public;

namespace QLNhanKhau
{
    public partial class UCUpdateKhaiSinh : UserControl
    {
        public UCUpdateKhaiSinh()
        {
            InitializeComponent();
        }

        private void UCUpdateKhaiSinh_Load(object sender, EventArgs e)
        {
            cmbDanToc.DataSource = DanTocBLL.LoadDanToc();
            cmbDanToc.DisplayMember = "TenDanToc";
            cmbDanToc.ValueMember = "MaDT";
            //load quoc tich
            cmbQuocTich.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTich.DisplayMember = "TenQuocTich";
            cmbQuocTich.ValueMember = "MaQT";
            //load quan hệ chủ hộ
            //cmbQuanHeChuHo.DataSource = QuanHeBLL.LoadQuanHe();
            //cmbQuanHeChuHo.DisplayMember = "TenQuanHe";
            //cmbQuanHeChuHo.ValueMember = "MaQH";
            //load quan hệ khai sinh
            cmbQuanHeKS.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHeKS.DisplayMember = "TenQuanHe";
            cmbQuanHeKS.ValueMember = "MaQH";

            cmbNguoiNhan.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiNhan.DisplayMember = "HoTen";
            cmbNguoiNhan.ValueMember = "MaND";

            //load cho phần khai sinh

            cmbNguoiCap.DataSource = NguoiDungBLL.LoadChuTich();
            cmbNguoiCap.DisplayMember = "HoTen";
            cmbNguoiCap.ValueMember = "MaND";

            cmbCanBoHoTich.DataSource = NguoiDungBLL.LoadCanBoHoTich();
            cmbCanBoHoTich.DisplayMember = "HoTen";
            cmbCanBoHoTich.ValueMember = "MaND";

            
            //to màu
            cmbNoiSinh.DataSource = TinhBLL.LoadTinh();
            cmbNoiSinh.DisplayMember = "TenTinh";
            cmbNoiSinh.ValueMember = "MaT";
            

            //dgvNhanKhau.DataSource = NhanKhauBLL.LoadNhanKhauKS();
            ////dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
            //dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
            ////dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
            //dgvNhanKhau.Columns[4].HeaderText = "CMND";
            //dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
            ////dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
            //dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
            //dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
            //dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
            //dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
            //dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
            ////dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
            //dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
            //dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
            ////dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
            //dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
            //dgvNhanKhau.Columns[0].Visible = false;
            ////dgvNhanKhau.Columns[1].Visible = false;
            //dgvNhanKhau.Columns[2].Visible = false;
            //dgvNhanKhau.Columns[3].Visible = false;
            ////dgvNhanKhau.Columns[4].Visible = false;
            ////dgvNhanKhau.Columns[5].Visible = false;
            //dgvNhanKhau.Columns[6].Visible = false;
            ////dgvNhanKhau.Columns[7].Visible = false;
            ////dgvNhanKhau.Columns[8].Visible = false;
            //dgvNhanKhau.Columns[9].Visible = false;
            ////dgvNhanKhau.Columns[10].Visible = false;
            //dgvNhanKhau.Columns[11].Visible = false;
            //dgvNhanKhau.Columns[12].Visible = false;
            //dgvNhanKhau.Columns[13].Visible = false;
            //dgvNhanKhau.Columns[14].Visible = false;
            //dgvNhanKhau.Columns[15].Visible = false;
            ////dgvNhanKhau.Columns[16].Visible = false;
            //dgvNhanKhau.Columns[17].Visible = false;
            //dgvNhanKhau.Columns[18].Visible = false;
            //dgvNhanKhau.Columns[19].Visible = false;
            //dgvNhanKhau.Columns[20].Visible = false;
            //dgvNhanKhau.Columns[21].Visible = false;
            ////dgvNhanKhau.Columns[22].Visible = false;
        }
        int _MaKS;

        private void dgvNhanKhau_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewCell cell = null;

            foreach (DataGridViewCell select in dgvNhanKhau.SelectedCells)
            {
                cell = select;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;
                _MaKS = (int)row.Cells[0].Value;
                txtHoTen.Text = (string)row.Cells[1].Value;
                dateNgaySinh.Value = ((DateTime)row.Cells[5].Value);
                if ((bool)row.Cells[7].Value == true)
                    sbtGioiTinh.Value = true;
                else
                    sbtGioiTinh.Value = false;
                cmbDanToc.SelectedValue = (byte)row.Cells[9].Value;
                cmbQuocTich.SelectedValue = (byte)row.Cells[11].Value;
                cmbNoiSinh.SelectedValue = (int)row.Cells[6].Value;
                txtQueQuan.Text = row.Cells[8].Value.ToString();

                KhaiSinhPublic ks = KhaiSinhBLL.LoadKhaiSinhByMaKS((int)row.Cells[2].Value);

                txtSoKhaiSinh.Text = ks.SoKhaiSinh.ToString();
                cmbQuanHeKS.SelectedValue = ks.MaQH;
                cmbCanBoHoTich.SelectedValue = ks.CanBoHoTich;
                txtNguoiDangki.Text = ks.NguoiDangki;
                cmbNguoiCap.SelectedValue = ks.NguoiCap;
                dateNgayCap.Value = (DateTime)ks.NgayCap;
                cmbCanBoDangKi.Text = NguoiDungBLL.LoadNguoiDungByMaND(ks.CanBoDangKi).HoTen;
                cmbNguoiNhan.SelectedValue = ks.NguoiNhan;
                txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND(ks.NguoiNhan).MaCV).TenChucVu;
                dateNgayNhap.Value = (DateTime)ks.NgayNhap;
                txtNoiCap.Text = ThietLapBLL.LoadDiaChiByMaDiaChi(ks.NoiCap);
                List<NhanKhauPublic> list_nkc = NhanKhauBLL.LoadNhanKhauByMaSHKCha((int)row.Cells[15].Value);
                List<NhanKhauPublic> list_nkm = NhanKhauBLL.LoadNhanKhauByMaSHKMe((int)row.Cells[15].Value);
                cmbHoTenCha.DataSource = list_nkc;
                cmbHoTenCha.DisplayMember = "HoTen";
                cmbHoTenCha.ValueMember = "MaNK";

                cmbHoTenCha.SelectedValue = ks.MaNKC;

                cmbHoTenMe.DataSource = list_nkm;
                cmbHoTenMe.DisplayMember = "HoTen";
                cmbHoTenMe.ValueMember = "MaNK";
                cmbHoTenMe.SelectedValue = ks.MaNKM;
               

                //cmbHoTenCha.SelectedValue = ks.MaNKC;
                //cmbHoTenMe.SelectedValue = ks.MaNKM;
                txtDanTocCha.Text = DanTocBLL.LoadDanTocByMaDT(NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKC).MaDT).TenDanToc;
                txtQuocTichCha.Text = QuocTichBLL.LoadQuocTichByMaQT(NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKC).MaQT).TenQuocTich;

                txtQuocTichMe.Text = QuocTichBLL.LoadQuocTichByMaQT(NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKM).MaQT).TenQuocTich;
                txtDanTocMe.Text = DanTocBLL.LoadDanTocByMaDT(NhanKhauBLL.LoadNhanKhauByMaNK(ks.MaNKM).MaDT).TenDanToc;

                dateNgayNhap.Value = (DateTime)ks.NgayNhap;
                txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND((int)ks.NguoiCap).MaCV).TenChucVu;
                txtNoiCap.Text = ThietLapBLL.LoadDiaChi();
                txtChucVuNguoiNhan.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND((int)ks.NguoiNhan).MaCV).TenChucVu;
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSoKS.Text == "")
            {
                MessageBox.Show("Vui lòng nhập số khai sinh", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoKS.Focus();
                return;
            }
            KhaiSinhPublic ks = KhaiSinhBLL.LoadKhaiSinhBySoKS(int.Parse(txtSoKS.Text));
            if (ks == null)
            {
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoKS.ResetText();
                txtSoKS.Focus();
                txtSoKhaiSinh.ResetText();
                txtNoiCap.ResetText();
                txtQueQuan.ResetText();
                cmbDanToc.Refresh();
                cmbNguoiCap.Refresh();
                cmbNguoiNhan.Refresh();
                cmbNoiSinh.Refresh();
                cmbQuanHeKS.Refresh();
                cmbQuocTich.Refresh();
                cmbHoTenCha.Refresh();
                cmbHoTenMe.Refresh();
                txtHoTen.ResetText();
                dateNgaySinh.ResetValue();
                dateNgayCap.ResetValue();
                dateNgayNhap.ResetValue();
                txtNguoiDangki.ResetText();
                cmbCanBoDangKi.Refresh();
                dgvNhanKhau.DataSource = null;
                return;
            }
            List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadListNhanKhauByMaKS(ks.MaKS);
            dgvNhanKhau.DataSource = list_nk;
        }

        private void cmbHoTenCha_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NhanKhauPublic father = NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenCha.SelectedValue);

            txtDanTocCha.Text = DanTocBLL.LoadDanTocByMaDT(father.MaDT).TenDanToc;
            txtDanTocCha.Enabled = false;
            txtQuocTichCha.Text = QuocTichBLL.LoadQuocTichByMaQT(father.MaQT).TenQuocTich;
            txtQuocTichCha.Enabled = false;
        }

        private void cmbHoTenMe_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NhanKhauPublic mother = NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenMe.SelectedValue);

            txtDanTocMe.Text = DanTocBLL.LoadDanTocByMaDT(mother.MaDT).TenDanToc;
            txtDanTocMe.Enabled = false;
            txtQuocTichMe.Text = QuocTichBLL.LoadQuocTichByMaQT(mother.MaQT).TenQuocTich;
            txtQuocTichMe.Enabled = false;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtSoKhaiSinh.Text == "" || dateNgayCap.Value ==null || txtNguoiDangki.Text == "" || txtQueQuan.Text == "" || txtHoTen.Text == "" || dateNgaySinh.Value == null || txtNoiCap.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông báo nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            byte ChucVu=NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV;
            byte ChucVuNguoiNhan=NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue).MaCV;
            int SoKhaiSinh =int.Parse(txtSoKhaiSinh.Text);
            KhaiSinhBLL.UpdateKhaisinh(SoKhaiSinh, (int)cmbNoiSinh.SelectedValue, (int)cmbHoTenCha.SelectedValue, (int)cmbHoTenMe.SelectedValue, txtNguoiDangki.Text, (byte)cmbQuanHeKS.SelectedValue, (int)cmbCanBoHoTich.SelectedValue, (int)cmbNguoiCap.SelectedValue, (DateTime)dateNgayCap.Value, (int)cmbNguoiNhan.SelectedValue, ChucVu, ChucVuNguoiNhan,_MaKS);
            NhanKhauBLL.UpdateNhanKhauKS(txtHoTen.Text, dateNgaySinh.Value, (int)cmbNoiSinh.SelectedValue, (byte)cmbDanToc.SelectedValue, (byte)cmbQuocTich.SelectedValue, (bool)sbtGioiTinh.Value, txtQueQuan.Text, _MaKS);
            MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoKS.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTim_Click(sender, e);
        }

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtHoTen.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        //private void txtNoiSinh_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (txtNoiSinh.Text.Length > 49 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtQueQuan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtQueQuan.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        //private void txtChucVu_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (txtChucVu.Text.Length > 15 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtNoiCap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtNoiCap.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbNguoiCap_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVu.Enabled = false;
        }

        private void txtNguoiDangKi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtNguoiDangki.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoKhaiSinh_Leave(object sender, EventArgs e)
        {
            if (KhaiSinhBLL.TestSoKhaiSinh(int.Parse(txtSoKhaiSinh.Text)) == false)
            {
                this.balloonTip2.ShowBalloon(txtSoKhaiSinh);
                this.balloonTip2.SetBalloonCaption(txtSoKhaiSinh, "Thông báo");
                this.balloonTip2.SetBalloonText(txtSoKhaiSinh, "Đã có Số khai sinh này trong hệ thống");
                txtSoKhaiSinh.Focus();
            }
        }

        private void txtSoKhaiSinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoKhaiSinh.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
